package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.config.Config;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.request.MarketingRequest;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.service.BcSinglesService;
import com.cloud.bc.service.BcUsersService;
import com.cloud.bc.util.BcUtils;
import com.cloud.bc.util.QRCodeUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 推荐好友
 */
@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center/marketing")
public class Ajax_MarketingController {
    private final BcUsersService bcUsersService;
    private final BcSinglesService bcSinglesService;
    private final Config config;
    private final QRCodeUtil qrCodeUtil;
    private final BcUtils bcUtils;

    /**
     * 生成海报
     */
    @GetMapping("getposter")
    public AjaxResp getposter(HttpServletRequest request, HttpServletResponse response) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        String eid = bcSinglesService.getEidEncode(userInfo.getId().toString());
        String url = config.getWebSite() + "m/recruit?eid=" + eid;
        String userId = bcSinglesService.getEidDecode(eid);
        String filePath = bcUtils.createOssFiledir("marketing_poster", "png", userId);
        String qrCode2Oss = qrCodeUtil.createQRCode2Oss(url, response, filePath);
        ajaxResp.setObj(qrCode2Oss);
        return ajaxResp;
    }

    /**
     * 我推荐的人
     */
    @GetMapping("team")
    public AjaxResp team(HttpServletRequest request, HttpServletResponse response,
                         MarketingRequest req) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        Page p = new Page();
        p.setCurrent(Convert.toInt(req.getPage(), 1));
        BcSingles bcSingles = new BcSingles();
        bcSingles.setUserEid(userInfo.getId().toString());
        bcSingles.setGender(req.getGender());
        IPage<BcSingles> bcSinglesIPage = bcSinglesService.selectPage(p, bcSingles);
        List<BcSingles> records = bcSinglesIPage.getRecords();
        if (null != records && records.size() > 0) {
            for (BcSingles record : records) {
                record.setAge(bcSinglesService.ageConvert(record.getBirthday()));
            }
        }
        ajaxResp.setList(bcSinglesIPage.getRecords());
        return ajaxResp;
    }

}
